/*
 * @Author: 黑白灰01 3193273703@qq.com
 * @Date: 2025-09-01 14:33:44
 * @LastEditors: 黑白灰01 3193273703@qq.com
 * @LastEditTime: 2025-09-01 14:33:51
 * @FilePath: /hei-oa/src/common/interceptors/transform.interceptor.ts
 * @Description: 拦截器
 */
// src/common/interceptors/transform.interceptor.ts
import {
    CallHandler,
    ExecutionContext,
    Injectable,
    NestInterceptor,
  } from '@nestjs/common';
  import { Observable } from 'rxjs';
  import { map } from 'rxjs/operators';
  import { BaseResponse } from 'src/common/response/base-response.dto';
  import { RESPONSE_CODE, RESPONSE_MSG } from 'src/common/enums/code';
  
  @Injectable()
  export class TransformInterceptor<T> 
    implements NestInterceptor<T, BaseResponse<T>> 
  {
    intercept(
      context: ExecutionContext,
      next: CallHandler
    ): Observable<BaseResponse<T>> {
      const ctx = context.switchToHttp();
      const request = ctx.getRequest();
      const path = request.url;
  
      return next.handle().pipe(
        map((data) => {
          // 已经封装为BaseResponse的响应不重复处理
          if (data instanceof BaseResponse) {
            return data;
          }
          
          return BaseResponse.success<T>(data, path);
        })
      );
    }
  }